package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.model.auto.IndexBanner;
import com.model.auto.IndexCulture;
import com.model.auto.Picture;
import com.service.IIndexBannerService;
import com.service.IIndexCultureService;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 首页
 * @author zhaoyifan
 * @date 2023/10/19 16:08
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    IIndexBannerService indexBannerService;
    @Autowired
    IIndexCultureService indexCultureService;

    /**
     * 获取首页banner
     *
     * @param type 类型：1：首页
     * @return
     */
    @GetMapping("/getBanner")
    public R getBanner(Integer type) {
        List<IndexBanner> list = indexBannerService.list(new LambdaQueryWrapper<IndexBanner>()
                .eq(IndexBanner::getType, type)
                .orderByAsc(IndexBanner::getSortNum)
                .eq(IndexBanner::getDisplay, 1)
        );
        return R.ok(list);
    }

    /**
     * 获取企业文化||公司产品
     * @param type  1：企业文化2：公司产品 3：公司介绍
     * @return
     */
    @GetMapping("/getCulture")
    public R getCulture(Integer type) {
        List<IndexCulture> list = indexCultureService.list(new LambdaQueryWrapper<IndexCulture>()
                .eq(IndexCulture::getType, type)
                .orderByAsc(IndexCulture::getSortNum)
                .eq(IndexCulture::getDisplay, 1)
        );
        if(Objects.nonNull(list)){
            list.forEach(item->{
                if(StringUtils.hasText(item.getDescription())){
                    item.setDescription(item.getDescription().replaceAll("\\n", "</br>").replaceAll("\\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
                }
            });
        }
        return R.ok(list);
    }
}
